prepare("SELECT id FROM students WHERE reg_number = ?"); $stmt->execute([$reg_number]); if ($stmt->rowCount() > 0) { $error = "Registration number already exists! Please use a different registration number."; } else { $stmt = $pdo->prepare("INSERT INTO students (reg_number, full_name, email, phone, password) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$reg_number, $full_name, $email, $phone, $password]); $success = "Student registered successfully!"; } } catch (PDOException $e) { $error = "Error registering student: " . $e->getMessage(); } } // Handle student update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_student'])) { $student_id = sanitize_input($_POST['student_id']); $reg_number = sanitize_input($_POST['reg_number']); $full_name = sanitize_input($_POST['full_name']); $email = sanitize_input($_POST['email']); $phone = sanitize_input($_POST['phone']); $password = $_POST['password'] ? password_hash($_POST['password'], PASSWORD_DEFAULT) : null; try { // Check if registration number already exists (excluding current student) $stmt = $pdo->prepare("SELECT id FROM students WHERE reg_number = ? AND id != ?"); $stmt->execute([$reg_number, $student_id]); if ($stmt->rowCount() > 0) { $error = "Registration number already exists! Please use a different registration number."; } else { if ($password) { $stmt = $pdo->prepare("UPDATE students SET reg_number = ?, full_name = ?, email = ?, phone = ?, password = ? WHERE id = ?"); $stmt->execute([$reg_number, $full_name, $email, $phone, $password, $student_id]); } else { $stmt = $pdo->prepare("UPDATE students SET reg_number = ?, full_name = ?, email = ?, phone = ? WHERE id = ?"); $stmt->execute([$reg_number, $full_name, $email, $phone, $student_id]); } $success = "Student updated successfully!"; } } catch (PDOException $e) { $error = "Error updating student: " . $e->getMessage(); } } // Handle student deletion if (isset($_GET['delete_id'])) { $delete_id = sanitize_input($_GET['delete_id']); try { // Check if student has exam attempts $stmt = $pdo->prepare("SELECT COUNT(*) as attempt_count FROM exam_sessions WHERE student_id = ?"); $stmt->execute([$delete_id]); $result = $stmt->fetch(PDO::FETCH_ASSOC); if ($result['attempt_count'] > 0) { $error = "Cannot delete student that has exam attempts. Please delete the exam attempts first."; } else { $stmt = $pdo->prepare("DELETE FROM students WHERE id = ?"); $stmt->execute([$delete_id]); $success = "Student deleted successfully!"; } } catch (PDOException $e) { $error = "Error deleting student: " . $e->getMessage(); } } // Handle bulk actions if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['bulk_action'])) { $bulk_action = sanitize_input($_POST['bulk_action']); $selected_students = $_POST['selected_students'] ?? []; if (empty($selected_students)) { $error = "Please select at least one student to perform bulk action."; } else { try { $placeholders = str_repeat('?,', count($selected_students) - 1) . '?'; if ($bulk_action === 'delete') { // Check if any selected student has exam attempts $stmt = $pdo->prepare("SELECT COUNT(*) as attempt_count FROM exam_sessions WHERE student_id IN ($placeholders)"); $stmt->execute($selected_students); $result = $stmt->fetch(PDO::FETCH_ASSOC); if ($result['attempt_count'] > 0) { $error = "Cannot delete students that have exam attempts. Please remove their exam attempts first."; } else { $stmt = $pdo->prepare("DELETE FROM students WHERE id IN ($placeholders)"); $stmt->execute($selected_students); $success = count($selected_students) . " student(s) deleted successfully!"; } } elseif ($bulk_action === 'export') { // Export functionality would go here $success = "Export functionality for " . count($selected_students) . " student(s) would be implemented here."; } } catch (PDOException $e) { $error = "Error performing bulk action: " . $e->getMessage(); } } } // Get search parameters $search = $_GET['search'] ?? ''; $sort = $_GET['sort'] ?? 'created_at'; $order = $_GET['order'] ?? 'desc'; // Build query for students with statistics $query = " SELECT s.*, COUNT(es.id) as exam_attempts, COUNT(r.id) as completed_exams, MAX(r.submitted_at) as last_activity, AVG(r.percentage) as average_score FROM students s LEFT JOIN exam_sessions es ON s.id = es.student_id LEFT JOIN results r ON es.id = r.session_id "; $params = []; if ($search) { $query .= " WHERE s.reg_number LIKE ? OR s.full_name LIKE ? OR s.email LIKE ?"; $search_term = "%$search%"; $params = [$search_term, $search_term, $search_term]; } $query .= " GROUP BY s.id"; // Add sorting $allowed_sorts = ['reg_number', 'full_name', 'created_at', 'exam_attempts', 'average_score']; $allowed_orders = ['asc', 'desc']; $sort = in_array($sort, $allowed_sorts) ? $sort : 'created_at'; $order = in_array($order, $allowed_orders) ? $order : 'desc'; $query .= " ORDER BY $sort $order"; $stmt = $pdo->prepare($query); $stmt->execute($params); $students = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get student for editing $edit_student = null; if (isset($_GET['edit_id'])) { $edit_id = sanitize_input($_GET['edit_id']); $stmt = $pdo->prepare("SELECT * FROM students WHERE id = ?"); $stmt->execute([$edit_id]); $edit_student = $stmt->fetch(PDO::FETCH_ASSOC); } // Get total statistics $total_students = count($students); $active_students = array_filter($students, function($student) { return $student['exam_attempts'] > 0; }); $total_attempts = array_sum(array_column($students, 'exam_attempts')); ?> Manage Students - Admin

Manage Students

Register, view, and manage student accounts

Total Students

Registered in system

Active Students

Taken at least one exam

Total Attempts

Exam sessions started

%

Average Score

Across all attempts

required placeholder="">
Only enter if you want to change the password Minimum 6 characters
❌ Cancel

All Students ()

0): ?>
Student Contact Info Statistics Last Activity Actions

📧
📞
Joined:
📊 attempts completed 🎯 % avg
No activity
✏️ Edit 📈 Results
👨‍🎓

No Students Found

View All Students